package com.welkom.everydate.dao;


import java.util.HashMap;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.welkom.everydate.domain.Authority;

@Repository
public class AuthorityDao extends BaseDao {
	public static final String GET_AUTHORITY_LIST_BY_USER_NAME="getAuthoritiesByUsername";
	public static final String GET_ALL_AUTHORITY_LIST="getAuthoritiesByUsername";
	public static final String GET_ALL_AUTHORITIES="getAllAuthorities";
	public static final String GET_AUTHORITY_BY_ID="getAuthorityByAuthorityId";
	public static final String GET_AUTHORITIES_BY_ROLE_ID="getAuthoritiesByRoleId";
	public static final String QUERY_AUTHORITIES_BY_PAGE="queryAuthoritiesByPage";
	public static final String GET_AUTHORITIES_BY_CONDITION="queryAuthoritiesCountByCondition";
	public static final String UPDATE_AUTHORITY_BY_ID="updateAuthorityById";
	public static final String INSERT_AUTHORITY="insertAuthority";
	public static final String DELETE_AUTHORITY_BY_ID="deleteAuthorityById";
	public static final String REMOVE_ROLE_AUTHORITY_RELATIONS_BY_ROLE_ID="removeRoleAuthorityRelationsByRoleId";
	public static final String ADD_ROLE_AUTHORITY_RELATION="addRoleAuthorityRelation";
	@Resource
	ResourceDao resourceDao;
	
	public List<Authority> getAuthoritiesByUserName(String userName){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("user_name", userName);
		List<Authority> authorities= (List<Authority>)getSqlMapClientTemplate().queryForList(GET_AUTHORITY_LIST_BY_USER_NAME, parameters);
		if(authorities!=null&&authorities.size()!=0){
			for(Authority authority:authorities){
				authority.setResources(resourceDao.getResourcesByAuthorityId(authority.getAuthorityId()));
			}
		}
		return authorities;
	}
	
	public List<Authority> getAllAuthorities(){
		List<Authority> authorities= (List<Authority>)getSqlMapClientTemplate().queryForList(GET_ALL_AUTHORITIES);
		if(authorities!=null&&authorities.size()!=0){
			for(Authority authority:authorities){
				authority.setResources(resourceDao.getResourcesByAuthorityId(authority.getAuthorityId()));
			}
		}
		return authorities;
	}
	
	public Authority getAuthorityById(long authorityId){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("authority_id", authorityId);
		Authority authority= (Authority)getSqlMapClientTemplate().queryForObject(GET_AUTHORITY_BY_ID, parameters);
		if(authority!=null){
			authority.setResources(resourceDao.getResourcesByAuthorityId(authority.getAuthorityId()));
		}
		return authority;
	}
	
	public List<Authority> getAuthoritiesByRoleId(long roleId){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("role_id", roleId);
		List<Authority> authorities= (List<Authority>) getSqlMapClientTemplate().queryForList(GET_AUTHORITIES_BY_ROLE_ID, parameters);
		if(authorities!=null&&authorities.size()!=0){
			for(Authority authority:authorities){
				authority.setResources(resourceDao.getResourcesByAuthorityId(authority.getAuthorityId()));
			}
		}
		return authorities;
	}
	
	public List<Authority> queryAuthoritiesByPage(int startIndex,int pageSize,Authority authority){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("startIndex", startIndex);
		parameters.put("pageSize", pageSize);
		if(authority!=null){
		parameters.put("authorityName", authority.getAuthorityName());
		parameters.put("authorityEnabled", authority.getAuthorityEnabled());
		}
		return (List<Authority>)getSqlMapClientTemplate().queryForList(QUERY_AUTHORITIES_BY_PAGE, parameters);
	}
	
	public int getAuthoritiesCountByCondition(Authority authority){
		Map<String,Object> parameters=new HashMap<String,Object>();
		if(authority!=null){
			parameters.put("authorityName", authority.getAuthorityName());
			parameters.put("authorityEnabled", authority.getAuthorityEnabled());
		}
		return (Integer)getSqlMapClientTemplate().queryForObject(GET_AUTHORITIES_BY_CONDITION, parameters);
	}
	
	public void updateAuthority(Authority authority){
		getSqlMapClientTemplate().update(UPDATE_AUTHORITY_BY_ID, authority);
	}
	
	public void addAuthority(Authority authority){
		getSqlMapClientTemplate().insert(INSERT_AUTHORITY, authority);
	}
	
	public void deleteAuthorityById(long authorityId){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("authority_id", authorityId);
		getSqlMapClientTemplate().delete(DELETE_AUTHORITY_BY_ID, parameters);
	}
	
	public void removeRoleAuthorityRelationsByRoleId(long roleId){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("role_id", roleId);
		getSqlMapClientTemplate().delete(REMOVE_ROLE_AUTHORITY_RELATIONS_BY_ROLE_ID, parameters);
	}
	
	public void addRoleAuthorityRelation(List<Authority> authorities,long roleId){
		for(Authority authority:authorities){
			Map<String,Object> parameters=new HashMap<String,Object>();
			parameters.put("role_id", roleId);
			parameters.put("authority_id", authority.getAuthorityId());
			getSqlMapClientTemplate().insert(ADD_ROLE_AUTHORITY_RELATION, parameters);
		}
	}

}
